﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class Hotel
    {
        #region Common
        public eHotel Data;

        public Hotel()
        {
            Data = new eHotel();
		}

        ~Hotel()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cHotel objDAL = new cHotel();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cHotel objDAL = new cHotel();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cHotel objInfor = new cHotel(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.HotelID;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }
        //Update nhung cai ma eHotel Data dang nam giu thong qua cHotel objInfor
        public int Update(int ID)
        {
            int nResult = 0;
            cHotel objInfor = new cHotel();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int UpdateStatus(int ID, int Status)
        {
            int nResult = 0;
            cHotel objDAL = new cHotel();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.Status = Status;
             
                objDAL.Save();
                nResult = objDAL.HotelID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public int UpdatePriority(int HotelID, int Priority)
        {
            int nResult = 0;
            cHotel objDAL = new cHotel();
            try
            {
                objDAL.LoadByPrimaryKey(HotelID);
                objDAL.Priority = Priority;

                objDAL.Save();
                nResult = objDAL.HotelID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }
        private void MappingData(cHotel objInfor)
        {
            try{objInfor.Address2 = Data.Address2;}
            catch { objInfor.Address2 = ""; }
            try{objInfor.Address1 = Data.Address1;}
            catch { objInfor.Address1 = ""; }
            try { objInfor.Address3 = Data.Address3; }
            catch { objInfor.Address3 = ""; }
            try { objInfor.Content1 = Data.Content1; }
            catch { objInfor.Content1 = ""; }
            try { objInfor.Content2 = Data.Content2; }
            catch { objInfor.Content2 = ""; }
            try { objInfor.Content3 = Data.Content3; }
            catch { objInfor.Content3 = ""; }
            try { objInfor.GoogleMapAddress = Data.GoogleMapAddress; }
            catch { objInfor.GoogleMapAddress = ""; }
            try { objInfor.Image = Data.Image; }
            catch { objInfor.Image = ""; }
            try { objInfor.InitContent1 = Data.InitContent1; }
            catch { objInfor.InitContent1 = ""; }
            try { objInfor.InitContent2 = Data.InitContent2; }
            catch { objInfor.InitContent2 = ""; }
            try { objInfor.InitContent3 = Data.InitContent3; }
            catch { objInfor.InitContent3 = ""; }
            try { objInfor.PriceFrom1 = Data.PriceFrom1; }
            catch { objInfor.PriceFrom1 = 0; }
            try { objInfor.PriceFrom2 = Data.PriceFrom2; }
            catch { objInfor.PriceFrom2 = 0; }
            try { objInfor.PriceFrom3 = Data.PriceFrom3; }
            catch { objInfor.PriceFrom3 = 0; }
            try { objInfor.PriceFromTo1 = Data.PriceFromTo1; }
            catch { objInfor.PriceFromTo1 = 0; }
            try { objInfor.PriceFromTo2 = Data.PriceFromTo2; }
            catch { objInfor.PriceFromTo2 = 0; }
            try { objInfor.PriceFromTo3 = Data.PriceFromTo3; }
            catch { objInfor.PriceFromTo3 = 0; }
            try { objInfor.Priority = Data.Priority; }
            catch { objInfor.Priority = 0; }
            try { objInfor.StarRating = Data.StarRating; }
            catch { objInfor.StarRating = 0; }
            try { objInfor.Status = Data.Status; }
            catch { objInfor.Status = 0; }
            try { objInfor.Title1 = Data.Title1; }
            catch { objInfor.Title1 = ""; }
            try { objInfor.Title2 = Data.Title2; }
            catch { objInfor.Title2 = ""; }
            try{ objInfor.Title3 = Data.Title3; }
            catch { objInfor.Title3 = ""; }
            try { objInfor.CityID = Data.CityID; }
            catch { objInfor.CityID = 0; }
            
        }
        private void ConvertData(cHotel objInfor)
        {
            try { Data.Address1 = objInfor.Address1; }
            catch { Data.Address1 = ""; }
            try { Data.Address2 = objInfor.Address2; }
            catch { Data.Address2 = ""; }
            try { Data.Address3 = objInfor.Address3; }
            catch { Data.Address3 = ""; }
            try { Data.Content1 = objInfor.Content1; }
            catch { Data.Content1 = ""; }
            try { Data.Content2 = objInfor.Content2; }
            catch { Data.Content2 = ""; }
            try { Data.Content3 = objInfor.Content3; }
            catch { Data.Content3 = ""; }
            try { Data.GoogleMapAddress = objInfor.GoogleMapAddress; }
            catch { Data.GoogleMapAddress = ""; }
            try { Data.Image = objInfor.Image; }
            catch { Data.Image = ""; }
            try { Data.InitContent1 = objInfor.InitContent1; }
            catch { Data.InitContent1 = ""; }
            try { Data.InitContent2 = objInfor.InitContent2; }
            catch { Data.InitContent2 = ""; }
            try { Data.InitContent3 = objInfor.InitContent3; }
            catch { Data.InitContent3 = ""; }
            try { Data.PriceFrom1 = objInfor.PriceFrom1; }
            catch { Data.PriceFrom1 = 0; }
            try { Data.PriceFrom2 = objInfor.PriceFrom2; }
            catch { Data.PriceFrom2 = 0; }
            try { Data.PriceFrom3 = objInfor.PriceFrom3; }
            catch { Data.PriceFrom3 = 0; }
            try { Data.PriceFromTo1 = objInfor.PriceFromTo1; }
            catch { Data.PriceFromTo1 = 0; }
            try { Data.PriceFromTo2 = objInfor.PriceFromTo2; }
            catch { Data.PriceFromTo2 = 0; }
            try { Data.PriceFromTo3 = objInfor.PriceFromTo3; }
            catch { Data.PriceFromTo3 = 0; }
            try { Data.Priority = objInfor.Priority; }
            catch { Data.Priority = 0; }
            try { Data.StarRating = objInfor.StarRating; }
            catch { Data.StarRating = 0; }
            try { Data.Status = objInfor.Status; }
            catch { Data.Status = 0; }
            try { Data.Title1 = objInfor.Title1; }
            catch { Data.Title1 = ""; }
            try { Data.Title2 = objInfor.Title2; }
            catch { Data.Title2 = ""; }
            try { Data.Title3 = objInfor.Title3; }
            catch { Data.Title3 = ""; }
            try { Data.CityID = objInfor.CityID; }
            catch { Data.CityID = 0; }
        }
        public DataTable Search(int ItemView, string Title, int nRegion, int nCity, int HotelPopID, int nStarRating, int PageIndex, int PageSize, ref int PageCount)
        {
            DataTable tbResult = new DataTable();
            cHotel objDAL = new cHotel();
            tbResult = objDAL.Search(ItemView, Title, nRegion, nCity, HotelPopID, nStarRating, PageIndex, PageSize, ref  PageCount);
            return tbResult;
        }

        #endregion
    }
}
